package in.muton.edushare.poll.components;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.poll.panels.IPollPanelContainer;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;

public class DeleteButton extends Button {
	private static final long serialVersionUID = -3725979651148344585L;
	private IModel<Poll> pollModel;
	
	private IPollPanelContainer container;
	public DeleteButton(String id, IModel<Poll> model, IPollPanelContainer container) {
		super(id);
		pollModel = model;
		this.container = container;
		setDefaultFormProcessing(false);
	}

	@Override
	public boolean isVisible() {
		if(pollModel == null)
			return true;
		return pollModel.getObject().getCreator() == EduShareSession.get()
				.getLoggedInUser();
	}

	public void onSubmit() {
		if(pollModel != null)
			new PollsDAO(EduShareSession.get().getSessionORMInterface())
				.deletePoll(pollModel.getObject());
		container.gotoDeletedPage();
	}
}
